﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCELibrary.Current;
 
using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;


namespace ZF.BLL.Admin
{

    public partial class MemberList : ZF.BLL.Page.MMPage
    {
        /// <summary>
        /// 1普通会员 2专家顾问 100系统管理员
        /// </summary>
        [PageArgument(Key = "mt",CanNull=false)]
        public int MemberType { get; set; }
        [PageArgument(Key = "st",DefaultValue=-1)]
        public int Status { get; set; }


        protected override void CheckRight()
        {
            if (this.MemberType == 1)
                this.Module = ModuleType.MMember;
            else if (this.MemberType == 2)
                this.Module = ModuleType.ZJMember;
            else if (this.MemberType == 100)
                this.Module = ModuleType.SysAdmin;
            base.CheckRight();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
       
            if (!IsPostBack)
            {
                View();
            }
            CheckRight();


        }
        public void View()
        {
            ListBind();
        }
        /// <summary>
        /// 列表绑定
        /// </summary>
        public void ListBind()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {


                List<Expression<Func<Model.US_Member, bool>>> filterList = new List<Expression<Func<Model.US_Member, bool>>>();
                IQueryable<Model.US_Member> selector = dc.US_Member;
                if (this.MemberType == 1)
                    filterList.Add(p => p.Type == 1 && p.IsExpert == false);
                else if (this.MemberType == 2)
                    filterList.Add(p => p.Type == 1 && p.IsExpert == true);
                else if (this.MemberType == 100)
                    filterList.Add(p => p.Type == 100);
                if (this.Status != -1)
                    filterList.Add(p => p.Status == this.Status);
                foreach (var filter in filterList)
                {
                    selector = selector.Where(filter);
                }
                var selWhere = selector.Select(p =>
                    new
                    {
                        Member = p,
                    });
                var selPage = selWhere.Where(this.gvList).OrderBy(this.gvList.SortExpression)
                        .Skip(gvList.PageIndex * gvList.PageSize).Take(gvList.PageSize);
                this.gvList.RowCount = selWhere.Count();
                var source = selPage.ToList();
                this.gvList.DataSource = source;
                this.gvList.DataBind();
                if (MemberType == 2)
                {
                    for (int i = 2; i < this.gvList.Columns.Count; i++)
                    {
                        this.gvList.Columns[i].Visible = true;
                    }
                }
            }
        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListSorting(object sender, GridViewSortEventArgs e)
        {
            ListBind();
        }
        /// <summary>
        /// 换页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListPageChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvList.PageIndex = e.NewPageIndex;
            ListBind();
        }
        public void Delete(params int[] idArr)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {
                    dc.US_Member.DeleteAllOnSubmit(
                       dc.US_Member.Where(p => idArr.Contains(p.MemberId)));
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = MemberType == 100 ? "系统管理员" : "会员";
                if (isSuccess)
                    WarnFactory.CreateWarn(string.Format("删除{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();

            }
        }
        public void Delete(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            if (bc.CommandArgument == "select")
            {
                var idArrTemp = this.gvList.GetCheckKeys();
                if (idArrTemp.Length == 0)
                    throw SiteInfo.WarnFactory.CreateWarn("未选择任何记录");
                var idArr = idArrTemp.Select(p => p.Convert<int>()).ToArray();
                Delete(idArr);
            }
            else
            {
                Delete(bc.CommandArgument.Convert<int>());
            }
        }

        public void TJ(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
 
            var id =   bc.CommandArgument.Convert<int>();
 
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {
                    var members = dc.US_Member.Where(p => p.MemberId == id);
                    foreach (var member in members)
                        member.IsRecommend = !member.IsRecommend;
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = MemberType == 100 ? "系统管理员" : "会员";
                if (isSuccess)
                {

                    Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('操作成功')", true);
                    ListBind();
                    //WarnFactory.CreateWarn(string.Format("操作成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();
                }

            }
        }

        public void Cancel(params int[] idArrTemp)
        {
            if (idArrTemp.Length == 0)
                throw SiteInfo.WarnFactory.CreateWarn("未选择任何记录");
            var idArr = idArrTemp.Select(p => p.Convert<int>()).ToArray();
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {
                    dc.US_Member.Where(p => idArr.Contains(p.MemberId)).ToList().ForEach(p => p.IsExpert = false);
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = MemberType == 100 ? "系统管理员" : "会员";
                if (isSuccess)
                {
                    WarnFactory.CreateWarn("操作成功", WarnIcon.Success, WarnLink.Current).Alert();
                }

            }
        }

        public void Cancel(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            if (bc.CommandArgument == "cancel")
            {
                var idArrTemp = this.gvList.GetCheckKeys();
                var idArr = idArrTemp.Select(p => p.Convert<int>()).ToArray();
                Cancel(idArr);
            }
            else
            {
                Cancel(bc.CommandArgument.Convert<int>());
            }
        }
          public void Search(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            if (this.tbAccount.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("Member.Account.Contains(@0)", this.tbAccount.Text);
            if (this.tbName.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("Member.Contact.Contains(@0)", this.tbName.Text);
            if (this.ddlType.SelectedValue != "0")
                this.gvList.AddFilterExpression("Member.ExpertType=@0", this.ddlType.SelectedValue.Convert<int>());

            if (this.tbArea1.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("Member.Area=@0", this.tbArea1.Text);
            if (this.tbArea2.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("Member.Area1=@0", this.tbArea2.Text);


            ListBind();
        }
        public void AllList(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            ListBind();
        }
        public string GetStatus(int status)
        {
            switch (status)
            {
                case 1:
                    return "帐号可用";
                default:
                    return "帐号不用";
            }
        }
    }
}
